package com.directorio.controller.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.directorio.modelo.bean.Empresa;
import com.directorio.controller.FrontEnd;
import com.directorio.modelo.bean.Administrador;
import com.directorio.modelo.bean.Alumno;
import com.directorio.modelo.service.AlumnoService;
import com.directorio.modelo.service.EmpresaService;
import com.directorio.modelo.utiles.pojo.JsonData;
import com.google.gson.Gson;

public class AdministradorController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AlumnoService alumno = null;
	private static AdministradorController administradorIntegration = null;
	private EmpresaService empresaService = null;
    private HttpServletRequest request;
    private Gson json = null;
    private ServletConfig conf;
    
    public static AdministradorController getInstance(){
    	if(administradorIntegration==null){
        	administradorIntegration = new AdministradorController();
        }
    	return administradorIntegration;
    }
    public void init(ServletConfig config) throws ServletException {
    	this.conf = config;
		super.init(config);
		requestContext();
	}
    
    public AdministradorController() {
        super();
        requestContext();
    }
    private void requestContext() {
        System.out.println("Cargando Logica Usuarios...!");
        alumno = (AlumnoService) FrontEnd.CONTEXT.getBean("alumnoService");
        empresaService = (EmpresaService)FrontEnd.CONTEXT.getBean("empresaService");
        json = new Gson();
        System.out.println("Logica Administrador Cargado Correctamente...!");
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procestServlet(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procestServlet(request, response);
	}
	
	private void procestServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.request = request;
        String ret = "";
        boolean band = false;
        try{
        	
            int op = Integer.parseInt(request.getParameter("opt"));
            switch (op) {
            		case 1:ret = this.insertarAdministrador();break;
            		case 2:ret = this.modificarAdministrador();break;
            		case 3:ret = this.eliminarAdministrador();break;
    	            case 9: ret = this.aceptPublicRequerimient();break;
            		case 10:ret = this.getAlumnosPendientesAcceso();break;
            		case 11: ret = this.getEmpresasPendientesAcceso();break;
            		case 12: ret = this.agregaUsuariosSistema();break;            		
            		case 13: ret = this.json.toJson(this.getCantidades());break;
            		case 14: ret = this.getOfertasPendientes();break;
            		case 15: ret = this.eliminarUsuariosSistema();break;            		
            		case 30: ret = this.insertHabilidadItem();break; 
            		case 31: ret = this.insertHabilidad();break;
            	}
        	}catch(Exception ex){
        }
        response.getWriter().print(ret);        
     }
	private String getOfertasPendientes() {
		String ret = "";
        List<?> listReq = null;
        Map<String, String> map = new HashMap();
        HttpSession ses = this.request.getSession();
        String ruc = "";
        try{
        	map.put("_esc","");
        	map.put("_dsc","");
        	map.put("_std","01");
            listReq = empresaService.obtenerBusquedadRequerimientos(map);
            ret = json.toJson(listReq);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            ret = null;
        }
        return ret;
	}
	private String insertHabilidad() {
		Map<String, String> map = new HashMap<String, String>();
		int op = 0;
		try{
			map.put("_cd_sc",this.request.getParameter("cd_sc"));
			map.put("_dsc", this.request.getParameter("dsc"));
			map.put("_op", this.request.getParameter("op"));			
			if(this.request.getParameter("cd_hb")!=null){
				map.put("_cd_hb", this.request.getParameter("cd_hb"));
			}
			op = alumno.insertHabilidad(map);
		}catch(Exception ex){
			ex.printStackTrace();
		}		
		return json.toJson(new JsonData(op != 0 ? "ok" : null));
	}
	private String insertHabilidadItem() {
		Map map = new HashMap();
		int op = 0;
		Administrador administrador ;
		HttpSession ses = this.request.getSession();
		try{
			map.put("_cd_sc",this.request.getParameter("cd_sc"));
			map.put("_cd_hb", this.request.getParameter("cd_hb"));			
			map.put("_op", this.request.getParameter("op"));
			if(this.request.getParameter("dsc")!=null){
				map.put("_dsc", this.request.getParameter("dsc"));
			}
			if(this.request.getParameter("cd_it")!=null){
				map.put("_cd_it", this.request.getParameter("cd_it"));
			}
			op = alumno.insertHabilidadItem(map);
		}catch(Exception ex){
			ex.printStackTrace();
		}		
		return json.toJson(new JsonData(op != 0 ? "ok" : null));
	}
	private String agregaUsuariosSistema() {
		Map map = new HashMap();
		String op = "";
		Administrador administrador ;
		HttpSession ses = this.request.getSession();
		try{
			map.put("_usr",((Administrador)ses.getAttribute("administrador")).getCod());
			map.put("codigos", this.request.getParameter("cod"));
			map.put("oper", "01");
			op = alumno.aceptaSolicitudUser(map);
		}catch(Exception ex){
			ex.printStackTrace();
		}		
		return json.toJson(new JsonData(Integer.parseInt(op) != 0 ? "ok" : null));
	}
	private String eliminarUsuariosSistema() {
		Map map = new HashMap();
		String op = "";
		Administrador administrador ;
		HttpSession ses = this.request.getSession();
		try{
			map.put("_usr",((Administrador)ses.getAttribute("administrador")).getCod());
			map.put("codigos", this.request.getParameter("cod"));
			op = alumno.eliminarRequestUser(map);
		}catch(Exception ex){
			ex.printStackTrace();
		}		
		return json.toJson(new JsonData(Integer.parseInt(op) != 0 ? "ok" : null));
	}

	private String getEmpresasPendientesAcceso() {
		List<Empresa> list = null;
		String ret = "";
		try{
			list = empresaService.obtenerEmpresaAccesoPendienteAll();
			ret = json.toJson(list);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return ret;
	}


	private String getAlumnosPendientesAcceso() {
		String ret = "";
        Map map = new HashMap();
        try {
            map.put("_std","00");
            map.put("_cod","");
            map.put("_ap",this.request.getParameter("ape"));
            map.put("_nmb",this.request.getParameter("nmb"));
            map.put("_fac", this.request.getParameter("fac"));
            List<Alumno> list = this.alumno.obtenerAlumnoAll(map);
            ret = json.toJson(list);
        } catch (Exception ex) {
            json = null;
            ex.printStackTrace();
        }
        return ret;
	}
	private String eliminarAdministrador() {
		int op = 0;
		Administrador administrador = new Administrador();		
		try{
			administrador.setCod(""+this.request.getAttribute("cod"));
			op = alumno.eliminarAdministrador(administrador);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json.toJson(new JsonData(op != 0 ? "ok" : null));
	}
	private String aceptPublicRequerimient(){
    	int op = 0;
        HttpSession ses = this.request.getSession();
        String dni = "";
        List<String> listMail = null;
        Map map = new HashMap();
        try{
            map.put("_id_req", request.getParameter("id_req"));
//            map.put("_dni", ((Administrador)ses.getAttribute("administrador")).getCod());
            map.put("_ruc", request.getParameter("ruc"));
            listMail = empresaService.publicarAceptacionRequerEmpresa(map);
            String dat = ((String)listMail.get(0));
            if(dat.compareTo("0")==0 || dat==null ){
            	op = 0;
            }else{
            	op = 1;
            }
        }catch(Exception ex) {
            json = null;
            ex.printStackTrace();
        }
        return json.toJson(new JsonData(op != 0 ? "ok" : null));
	}
	
	private String modificarAdministrador() {
		int op = 0;
		Administrador administrador = new Administrador();
		HttpSession ses = this.request.getSession();		
		try{
			administrador.setCod(((Administrador)ses.getAttribute("administrador")).getCod());
			administrador.setStd(""+this.request.getAttribute("std"));
			administrador.setPwd(""+this.request.getAttribute("pwd"));
			administrador.setMail(""+this.request.getAttribute("mail"));
			administrador.setNomb(""+this.request.getAttribute("nmb"));
			op = alumno.modificarAdministrador(administrador);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json.toJson(new JsonData(op != 0 ? "ok" : null));
	}
	private String insertarAdministrador() {
		int op = 0;
		Administrador administrador = new Administrador();
		HttpSession ses = this.request.getSession();		
		try{
			administrador.setCod(((Administrador)ses.getAttribute("administrador")).getCod());
			administrador.setMail(""+this.request.getAttribute("mail"));
			administrador.setNomb(""+this.request.getAttribute("nmb"));
			op = alumno.agregarAdministrador(administrador);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return json.toJson(new JsonData(op != 0 ? "ok" : null));
	}	
	public List<String> getCantidades(){
		String cod = "";
		Map map = new HashMap();
		map.put("_cod",cod);
		try{
			return  alumno.obtenerCantidadesAdminInfor(map);
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
}
